package org.xhnews.ui;

import java.util.Vector;

import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;

import org.xhnews.bo.ForwardBo;
import org.xhnews.to.Contact;
import org.xhnews.to.NewsItem;

public class ForwardScreen extends MainScreen implements ScreenClosedListener {
	private String[] _strContacts;
	private Vector _receivers;
	NewsItem _newsitem;
	private InputBoxVerticalFieldManager _ib_messsageContent;
	private InputBoxVerticalFieldManager _ib_receiver;
	private InputBoxVerticalFieldManager _ib_subject;
	private TitleField _tf;
	ContactsSelectionScreen _css;
	final private ForwardScreen thisScreen;
	public ForwardScreen() {
		thisScreen=this; 
		_tf=new TitleField();
		_tf.setText("新闻转发");
		add(_tf);
		this.getMainManager().setBackground(
				BackgroundFactory.createLinearGradientBackground(0x0099CCFF,0x0099CCFF,0x00336699,0x00336699));
		_ib_messsageContent = new InputBoxVerticalFieldManager(this.getWidth(),this.getFont().getHeight()*5);
		_ib_receiver = new InputBoxVerticalFieldManager(this.getWidth(),this.getFont().getHeight()*2) {
			protected boolean navigationClick(int status, int time) {
				UiApplication.getUiApplication().invokeLater(new Runnable() {
					public void run() {
						_css=new ContactsSelectionScreen(thisScreen);
						UiApplication.getUiApplication().pushScreen(_css);
					}
				});
				return true;
			}
		};
		_ib_subject=new InputBoxVerticalFieldManager(this.getWidth(),this.getFont().getHeight()*2,EditField.NON_FOCUSABLE);
        add(_ib_receiver);
		add(_ib_subject);
		add(_ib_messsageContent);

		HorizontalFieldManager _fieldManagerBottom = new HorizontalFieldManager();
		ButtonField sendButton = new ButtonField("发送") {
			protected boolean navigationClick(int status, int time) {
				UiApplication.getUiApplication().invokeLater(new Runnable() {
					public void run() {
						String[] revType={"P","M"};
						String strResponse=ForwardBo.forward(_newsitem.getID(), _receivers,revType, _ib_messsageContent.getText());
						Dialog.inform(strResponse);
					}
				});
				return true;
			}
		};
		ButtonField cancelButton = new ButtonField("取消") {
			protected boolean navigationClick(int status, int time) {
				UiApplication.getUiApplication().invokeLater(new Runnable() {
					public void run() {
						close();
					}
				});
				return true;
			}
		};
		_fieldManagerBottom.add(sendButton);
		_fieldManagerBottom.add(cancelButton);
		add(_fieldManagerBottom);
	}

	public void initialize(NewsItem newsitem) {
		_newsitem=newsitem;
		setNewsItem(_newsitem);
		
	}
    public void setNewsItem(NewsItem newsitem){
    	_ib_subject.setText(newsitem.getTitle());
    }

	public class InputBoxVerticalFieldManager extends VerticalFieldManager{  
		  
	    private int managerWidth;  
	    private int managerHeight;  
	    private EditField editField;  
 	      	      
	    public InputBoxVerticalFieldManager(int width, int height) {  
	        super(Manager.NO_VERTICAL_SCROLL);  
	        managerWidth = width;  
	        managerHeight = height;  
	        VerticalFieldManager vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL);  
	        editField = new EditField();  
	        editField.setPadding(5,5,5,5);  
	        vfm.add(editField);  
	        add(vfm);  
	    }  
	    public InputBoxVerticalFieldManager(int width, int height,long style) {  
	        super(Manager.NO_VERTICAL_SCROLL);  
	        managerWidth = width;  
	        managerHeight = height;  
	        VerticalFieldManager vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL);  
	        editField = new EditField(style);  
	        editField.setPadding(5,5,5,5);  
	        vfm.add(editField);  
	        add(vfm);  
	    } 
	  
	    public void sublayout(int width, int height) {  
	        if(managerWidth == 0) {  
	            managerWidth = width;  
	        }  
	        if(managerHeight == 0) {  
	            managerHeight = height;  
	        }  
	        super.sublayout(managerWidth, managerHeight);  
	        setExtent(managerWidth,managerHeight);  
	    }  
	      
	    protected void paintBackground(Graphics graphics) {   
	    	graphics.setColor(Color.WHITE);
	        graphics.setStrokeWidth(2);
	        graphics.fillRoundRect(5, 5, getWidth()-10, getHeight()-5,10,10);
//	        setBorder(BorderFactory.createBitmapBorder(new XYEdges(2,2,2,2), borderBitmap));
	        graphics.setColor(Color.BLACK);
	        super.paint(graphics);  
	    }  
	      
	    public String getText() {  
	        return editField.getText();  
	    }  
	    public void setText(String text) {  
	        editField.setText(text);  
	    }  

	}  
	public void notifyScreenClosed(Screen screen) {
        if (screen instanceof ContactsSelectionScreen) {
        	_receivers=((ContactsSelectionScreen)screen).getSelectedUser();        	
        	_ib_receiver.setText(vectorOfContactsToString(_receivers));
 	  }
  }
	private String vectorOfContactsToString(Vector contacts){
		String usernames="";
		for(int i=0;i<contacts.size();i++){
			usernames+=((Contact)contacts.elementAt(i)).getName();
			usernames+=";";
		}
		return usernames;
	}
	protected boolean onSavePrompt() {
		return true;
	}

}
